package com.he.fetch.test;

import java.util.List;
import java.util.UUID;

import javax.persistence.EntityManager;

public class DataHelper {

    @SuppressWarnings("unchecked")
    protected static Object random(EntityManager em, Class clazz,
	    Integer maxResults) {
	List<Object> instances = em.createQuery(
		"SELECT o FROM " + clazz.getName() + " o").setMaxResults(
		maxResults == null ? 10 : maxResults).getResultList();
	int i = (int) (Math.random() * instances.size());
	System.out.println(i);
	return instances.isEmpty() ? null : instances.get(i);
    }

    protected static boolean randomBoolean() {
	return randomInt(2) % 2 == 0 ? true : false;
    }

    public static Double randomDouble(int range) {
	return (double) (Math.random() * range);
    }

    protected static int randomInt(int range) {
	return (int) (Math.random() * range);
    }

    protected static String randomString(int length) {
	String s = UUID.randomUUID().toString().replace("-", "");
	while (s.length() < length) {
	    s = s + UUID.randomUUID().toString().replace("-", "");
	}
	return s.substring(0, length);
    }

}
